package cn.tedu._04mvcweibo.controller;

import cn.tedu._04mvcweibo.common.response.JsonResult;
import cn.tedu._04mvcweibo.common.response.StatusCode;
import cn.tedu._04mvcweibo.mapper.CommentMapper;
import cn.tedu._04mvcweibo.pojo.dto.CommentDTO;
import cn.tedu._04mvcweibo.pojo.entity.Comment;
import cn.tedu._04mvcweibo.pojo.vo.CommentVO;
import cn.tedu._04mvcweibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "03.评论模块")
@Slf4j
@RestController //Controller注解 + ResponseBody注解
@RequestMapping("/v1/comment/")
public class CommentController {

    @Autowired(required = false)
    private CommentMapper commentMapper;

    /**
     * 发布评论功能
     */
    @ApiOperation(value = "发布评论功能")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        log.debug("commentDTO = " + commentDTO);

        /*
            1.校验用户登录状态
            2.发布评论[调用接口方法]
         */
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            //未登录
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        //发布评论逻辑[实体类Entity]
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setCreated(new Date());
        comment.setUserId(userVO.getId());
        // 调用接口方法
        commentMapper.insert(comment);

        return JsonResult.ok();
    }

    /**
     * 获取指定微博的评论功能
     * 参数id: 微博的ID
     */
    @ApiOperation(value = "获取评论功能")
    @GetMapping("selectByWeiboId")
    public JsonResult selectByWeiboId(int id){
        /*
            直接调用查询接口
         */
        List<CommentVO> list = commentMapper.selectByWeiboId(id);

        return JsonResult.ok(list);
    }
}



















